﻿using AutoMapper;
using B.S.BasicData.API.Read.Application.Command.Role;
using B.S.BasicData.API.Read.DTOS.UserRoleMenu;
using B.S.BasicData.Domain.Entities.UserRoleMenu;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.API.Read.Application.Handler.Role
{
    /// <summary>
    /// 获取角色信息请求命令处理类
    /// </summary>
    public class GetRoleHandler:IRequestHandler<GetRoleCommand,ApiResult<List<RoleDTO>>>
    {
        IBaseRepository<RoleModel> roleRep;
        IMapper mapper;

        public GetRoleHandler(IBaseRepository<RoleModel> roleRep, IMapper mapper)
        {
            this.roleRep = roleRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<RoleDTO>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<RoleDTO>> res = new ApiResult<List<RoleDTO>>();

                var roles = roleRep.GetAll().OrderByDescending(x=>x.Id).Where(x => x.IsDeleted == false);
                var entitys=mapper.Map<List<RoleDTO>>(roles);
                res.Msg = "获取角色信息";
                res.Code = ApiEnum.Success;
                res.Data = entitys.ToList();
                return Task.FromResult(res);


            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
